package com.bang.study.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Auther: Bang
 * @Date: 2020/8/2 20:06
 * @Description:
 */
@Component
@Aspect
public class LogAspect {

    @Pointcut(value = "execution(* com.bang.study.proxy.*.*(..))")
    public void pointCut() {

    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("before [ "+name+" ]参数: {"+ Arrays.asList(args)+"}");
    }

    @After("pointCut()")
    public void after(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("after [ "+name+" ]参数: {"+ Arrays.asList(args)+"}");
    }

    /*@Around(value = "pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        String name = pjp.getSignature().getName();
        System.out.println("before around "+name);
        Object proceed = pjp.proceed(pjp.getArgs());
        System.out.println(proceed);
        System.out.println("after around "+name);
        return proceed;
    }*/

    @AfterReturning(value = "pointCut()",returning = "rvt")
    public void afterReturning(JoinPoint joinPoint,Object rvt) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("afterReturning [ "+name+" ] 参数: {"+ Arrays.asList(args)+"} 返回值:{"+rvt+"}");
    }

    @AfterThrowing(value = "pointCut()",throwing = "exception")
    public void afterThrowing(JoinPoint joinPoint,Exception exception) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("afterThrowing [ "+name+" ] 参数: {"+ Arrays.asList(args)+"} EX:{ "+exception+" }");
    }

}
